package com.masterclient.app.modules.aftersale.contract;

import com.eoner.common.bean.base.CommonBaseBean;
import com.eoner.baselib.presenter.ThrowableConsumer;
import com.masterclient.app.httpservice.RetrofitUtil;
import com.eoner.baselibrary.bean.aftersale.AfterSaleBean;
import com.eoner.baselibrary.bean.aftersale.RefundBtnBean;

import io.reactivex.Observable;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
* Created by TMVPHelper on 2017/08/02
*/
public class AfterSaleDetailPresenter extends AfterSaleDetailContract.Presenter{

    @Override
    public void getAfterSaleDetail(String order_id, String order_product_id) {
        Observable<AfterSaleBean> observable = RetrofitUtil.getInstance().getAfterSales(order_id,order_product_id);
        Disposable disposable = toSubscribe(observable, new Consumer<AfterSaleBean>() {
            @Override
            public void accept(@NonNull AfterSaleBean bean) throws Exception {
                if(bean!=null&&bean.getData()!=null){
                    mView.showAfterSaleInfo(bean.getData());
                }
            }
        },new ThrowableConsumer());

        mRxManager.add(disposable);
    }

    @Override
    public void closeAfterSale(String refund_id) {
        Observable<CommonBaseBean> observable = RetrofitUtil.getInstance().closeAfterSale(refund_id);
        Disposable disposable = toSubscribe(observable, new Consumer<CommonBaseBean>() {
            @Override
            public void accept(@NonNull CommonBaseBean bean) throws Exception {
                if("0".equals(bean.getCode())){
                    mView.closeAfterSaleSuccess();
                }else {
                    mView.failed("关闭售后失败");
                }
            }
        },new ThrowableConsumer());

        mRxManager.add(disposable);
    }

    @Override
    public void getRefundBtn(String refund_id) {
        Observable<RefundBtnBean> observable = RetrofitUtil.getInstance().getRefundBtn(refund_id);
        Disposable disposable = toSubscribe(observable, new Consumer<RefundBtnBean>() {
            @Override
            public void accept(@NonNull RefundBtnBean bean) throws Exception {
                if("0".equals(bean.getCode())){
                    mView.showRefundBtn(bean);
                }else {
                    mView.getRefundBtnFail("获取操作状态失败");
                }
            }
        },new ThrowableConsumer());

        mRxManager.add(disposable);
    }


}